<?php

namespace App\Task\Recommend;

use App\Cache\RedisKey;
use App\Helpers\RedisUtil;
use App\Model\UserProfile;
use Hyperf\Crontab\Annotation\Crontab;
use RedisException;

/**
 * 每天定时修改亲密度
 * @Crontab(name="RecommendNewUserTask", rule="*\/15 * * * * *", callback="execute", memo="异步生成推荐新用户", singleton=false)
 */
class RecommendNewUserTask
{
    const EXP_TIME = 120;

    /**
     * @return void
     * @throws RedisException
     */
    public function execute()
    {
        $getGender = 1;
        $role = $getGender == 1 ? UserProfile::ROLE_USER : UserProfile::ROLE_HOST;

        $onlineData = (new UserProfile)->getNewUsersV2($role, $getGender, 1);
        $redis = RedisUtil::getRedis();
        $onlineKey = RedisKey::userRecommendNewUserKey($role, $getGender, 1);

        $redis->del($onlineKey);
        $redis->sAddArray($onlineKey, array_diff($onlineData, config('busi.kefu_uid')));
        $redis->expire($onlineKey, self::EXP_TIME);

        $noOnlineData = (new UserProfile)->getNewUsersV2($role, $getGender, 0);
        $noOnlineKey = RedisKey::userRecommendNewUserKey($role, $getGender, 0);

        $redis->del($noOnlineKey);
        $redis->sAddArray($noOnlineKey, array_diff($noOnlineData, config('busi.kefu_uid')));
        $redis->expire($noOnlineKey, self::EXP_TIME);

        return;
    }
}